var _default_timeout = 60 * 1000; // default timeout time in ms

var _openx_header = '_openx_header';
var _openx_conf = '_openx_conf';

var _self = this;
var _store = localStorage;

this.conf = function (conf) {
    if (!_self.isNull(conf)) {
        _store.setItem(_openx_conf, $.toJSON(conf));
    } else {
        var json = _store.getItem(_openx_conf);
        return $.evalJSON(json);
    }
};
this.isBlank = function (str) {
    if (_self.isNull(str)) {
        return true;
    } else return (str == '' || str.length == 0);
};

this.isNull = function (obj) {
    return (obj == null || obj == undefined);
};

this.head = function (key, val) {
    var header = _store.getItem(_openx_header);
    if (this.isBlank(header)) {
        header = '{"sessionId":"' + Math.uuid() + '"}';
        _store.setItem(_openx_header, header);
    }
    var json = $.evalJSON(header);
    // if write operation
    if (!this.isBlank(key)) {
        json[key] = val;
        _store.setItem(_openx_header, $.toJSON(json));
    }
    return json;
};

this.store = function (key, val) {
    if (this.isBlank(val)) {
        // get function
        return $.evalJSON(_store.getItem(key));
    } else {
        // put function
        _store.setItem(key, $.toJSON(val));
    }
};


var CommonUtil = {};

CommonUtil.token = '123456';


/**
 * 自定义ajax可同步 异步
 * @param options ｛timeout,url,type,contentType,data,success,error,｝
 */
CommonUtil.ajax = function (options) {
    var timeout = _default_timeout;
    var type = null;
    var code = 'json';
    var success = function (data) {
        console.log(data);
    };
    var error = function (err) {
        console.log(err);
    };
    var json;
    var url = options.url;
    if ((typeof options === 'object' && CommonUtil.isEmptyObject(options)) || typeof options != 'object') {
        alert('option 对象参数没有传递 比如url type 等');
        return;
    }
    if (_self.isBlank(options.url)) {
        alert('option url 不能为空');
    } else {
        url = options.url;
    }

    if (options.type == 'html' || options.type == 'app') {
        type = 'post';
        code = 'json';
        url = url + "?code=json";
    } else if (options.type == 'jsonp') {
        type = 'get';
        code = 'jsonp';
        url = url + "?code=jsonp"
    } else {
        alert('option.type 必须是 html app jsonp 中的一个');
        return;
    }

    if (typeof options.success == 'function') {
        success = options.success
    }
    if (typeof options.error == 'function') {
        error = options.error;
    }
    var data=options.data||{};
    data._openx_head = _self.head();
    json = $.toJSON(data);
    if (code == 'jsonp') {
        url = url + '&data=' + $.base64.encode(json);
        json = null;
    }


    var async = true;
    if (options.async === false) {
        async = false;
    }
    $.ajax({
        timeout: options.timeout || timeout,
        url: url,
        type: type || 'post',
        contentType: options.contentType || "application/json",
        data: json || {},
        dataType: options.dataType || 'json',
        async: async,
        success: success,
        error: error
    });
};

CommonUtil.isEmptyStr = function (obj) {
    var result = false;
    if (obj == undefined || obj == null || obj == '') {
        result = true;
    } else {
        if (obj.trim().length == 0) {
            result = true;
        }
    }
    return result;
}

CommonUtil.isEmptyCollection=function(obj){
    var result = false;
    if (obj == undefined || obj == null) {
        result = true;
    } else {
        if (obj.length == 0) {
            result = true;
        }
    }
    return result;
}

CommonUtil.isEmptyObject = function (obj) {
    var name;
    for (name in obj) {
        return false;
    }
    return true;
};

CommonUtil.isNullObject = function (obj) {
    for (var i in obj) {
        if (obj.hasOwnProperty(i)) {
            return false;
        }
    }
    return true;
};


/*本地存储*/
var uzStorage = function () {
    var ls = window.localStorage;
    return ls;
};
var uzSessionStorage = function () {
    var ls = window.sessionStorage;
    return ls;
};
/*
 设置本地离线数据
 @key 命名
 @value 值
 */
CommonUtil.setStorage = function (key, value) {
    if (arguments.length === 2) {
        var v = value;
        if (typeof v == 'object') {
            v = JSON.stringify(v);
            v = 'obj-' + v;
        } else {
            v = 'str-' + v;
        }
        var ls = uzStorage();
        if (ls) {
            ls.setItem(key, v);
        }
    }
};
CommonUtil.getStorage = function (key) {
    var ls = uzStorage();
    if (ls) {
        var v = ls.getItem(key);
        if (!v) {
            return;
        }
        if (v.indexOf('obj-') === 0) {
            v = v.slice(4);
            return JSON.parse(v);
        } else if (v.indexOf('str-') === 0) {
            return v.slice(4);
        }
    }
};
CommonUtil.rmStorage = function (key) {
    var ls = uzStorage();
    if (ls && key) {
        ls.removeItem(key);
    }
};
CommonUtil.clearStorage = function () {
    var ls = uzStorage();
    if (ls) {
        ls.clear();
    }
};
// sessionStorage
CommonUtil.setSessionStorage = function (key, value) {
    if (arguments.length === 2) {
        var v = value;
        if (typeof v == 'object') {
            v = JSON.stringify(v);
            v = 'obj-' + v;
        } else {
            v = 'str-' + v;
        }
        var ls = uzSessionStorage();
        if (ls) {
            ls.setItem(key, v);
        }
    }
};
CommonUtil.getSessionStorage = function (key) {
    var ls = uzSessionStorage();
    if (ls) {
        var v = ls.getItem(key);
        if (!v) {
            return;
        }
        if (v.indexOf('obj-') === 0) {
            v = v.slice(4);
            return JSON.parse(v);
        } else if (v.indexOf('str-') === 0) {
            return v.slice(4);
        }
    }
};
CommonUtil.rmSessionStorage = function (key) {
    var ls = uzSessionStorage();
    if (ls && key) {
        ls.removeItem(key);
    }
};
CommonUtil.clearSessionStorage = function () {
    var ls = uzSessionStorage();
    if (ls) {
        ls.clear();
    }
};

CommonUtil.getAuthToken=function () {
    var ls = uzStorage();
    if (ls) {
        var v = ls.getItem("_openx_header");
        if (!v) {
            return null;
        }
        var openx_header=JSON.parse(v);
        if(openx_header&&openx_header.auth_token){
            return openx_header.auth_token;
        }
        return null;
    }
}


